#ifndef EVENT_QUEUE
#define EVENT_QUEUE




//extern "C" __device__ void eventHeapInitial(Event* d_eventHeap,int* eventHeapSize)

template<class T> __device__ void heapInitial(T* d_heap,int& heapSize){
	heapSize = 0;
}

template<class T> __device__ void heapInsert(const T &tmpElement , T* d_heap,int& heapSize){
	heapSize++;
	int i = heapSize;
	int j = i / 2;
	while( j > 0 ){
		if( d_heap[j] < tmpElement ) break;
		else{
			d_heap[i] = d_heap[j];
			i=j;j/=2;
		}
	}
	d_heap[i] = (tmpElement);
}

template<class T> __device__ void heapPop(T &tmpElement,T* d_heap , int& heapSize ){
	if( heapSize == 0 ) {
		cuPrintf("empty heap !\n");
		return;
	}
	tmpElement = d_heap[1];
	heapSize--;
	int k = heapSize;
	int i = 1 , j = 2;
	while( j <= heapSize){
		if(j<heapSize&&d_heap[j+1]<d_heap[j])j++;
		if(d_heap[j] <d_heap[k] ){
			d_heap[i] = d_heap[j];
			i=j;j=j*2;
		}
		else break;
	}
	d_heap[i] = d_heap[k];
}

template<class T> __device__ void heapPop(T* d_heap , int& heapSize ){
	if( heapSize == 0 ) {
		cuPrintf("empty heap !\n");
		return;
	}
	heapSize--;
	int k = heapSize;
	int i = 1 , j = 2;
	while( j <= heapSize){
		if(j<heapSize&&d_heap[j+1]<d_heap[j])j++;
		if(d_heap[j] <d_heap[k] ){
			d_heap[i] = d_heap[j];
			i=j;j=j*2;
		}
		else break;
	}
	d_heap[i] = d_heap[k];
}

template<class T> __device__ T*  heapTop(T* d_heap,const int& heapSize )
{
	if( heapSize == 0 ){
		cuPrintf("heapempty!!\n");
		return NULL;
	}
	return &(d_heap[1]);
	
}

template<class T> __device__ bool heapEmpty(int heapSize)
{
	return heapSize == 0;
}




#endif